package com.lj.mcore.service;

import com.lj.mcore.dao.base.BaseAppDAO;
import com.lj.mcore.entity.Store;
import com.lj.mcore.entity.StoreClass;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StoreClassService implements IStoreClassService{

    @Resource(name="storeClassDao")
    private BaseAppDAO<StoreClass> storeClassDao;

    @Override
    public boolean save(StoreClass obj) {
        return storeClassDao.save(obj);
    }

    @Override
    public boolean delete(StoreClass obj) {
        return storeClassDao.delete(obj);
    }

    @Override
    public boolean update(StoreClass obj) {
        return storeClassDao.update(obj);
    }

    @Override
    public StoreClass getObjById(Long id) {
        return storeClassDao.findById(id);
    }

    @Override
    public StoreClass getSingleResult(Map<String, Object> map) {
        return storeClassDao.getSingleResult(map);
    }

    @Override
    public List<StoreClass> query(String paramString, Map paramMap, int paramInt1, int paramInt2) {
        return storeClassDao.query(paramString, paramMap, paramInt1, paramInt2);
    }

    @Override
    public PageBean getList(int pageSize, int page, Map<String, String> query) {
        StringBuffer sb = new StringBuffer("from StoreClass obj where obj.deleteStatus = false ");
        if(query != null && query.size() >0 ){

            if(!CommUtils.null2String(query.get("sid")).equals("")){
                sb.append(" and obj.store.id = "+query.get("sid"));
            }

            if(!CommUtils.null2String(query.get("name")).equals("")){
                sb.append(" and obj.className like '%"+query.get("name")+"%'");
            }

        }
        sb.append(" order by obj.sequence asc");

        String hql = sb.toString();

        List listNum = this.query("select count(obj.id) "+hql,null,0,1);
        int allRow=0;
        if (listNum!=null){
            allRow = CommUtils.null2Int(listNum.get(0));
        }
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List list = this.query(hql,null, offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
    }

    @Override
    public List<StoreClass> storeClassTree(long sid) {
        Map  query = new HashMap();
        query.put("sid",sid);
        query.put("display",1);
        return storeClassDao.query("from StoreClass obj where obj.deleteStatus = false and obj.store.id=:sid and obj.level = 0" +
                " and obj.display =:display order by obj.sequence asc",query,-1,-1);
    }
}
